import json
import sys
import pandas as pd
import numpy as np
from pandas import DataFrame,Series
import pylab

path = sys.path[0] + '/example.txt'
records = [json.loads(line) for line in open(path)]

print(records)

timezone = [rec['tz'] for rec in records if 'tz' in rec]

def get_counts(sequence):
    counts={}
    for item in sequence:
        if item in counts:
            counts[item] += 1
        else:
            counts[item] = 1
    return counts

def top_counts(count_dict,n=10):
    key_value_pairs = [(key,value) for key,value in count_dict.items()]
    key_value_pairs.sort(key=lambda x : (x[1],x[0]),reverse=True)
    return key_value_pairs[:n]

frame = DataFrame(records)
tz_counts = frame['tz'].value_counts()
print(tz_counts[:10])

clean_tz = frame['tz'].fillna('Missing')
print(clean_tz[:10])
clean_tz[clean_tz == ''] = 'Unknown'
tz_counts = clean_tz.value_counts()
tz_counts[:10].plot(kind='barh',rot=0)
